package com.cskaoyan.mall.user.controller;

import com.cskaoyan.mall.common.result.Result;
import com.cskaoyan.mall.common.util.IpUtil;
import com.cskaoyan.mall.user.dto.UserLoginDTO;
import com.cskaoyan.mall.user.query.UserInfoParam;
import com.cskaoyan.mall.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.UUID;

@RestController
public class SearchApiController {
    @Autowired
    UserService userService;

    @PostMapping("/user/login")
    public Result login(@RequestBody UserInfoParam userInfoParam, HttpServletRequest requestBody){
        //要先获取ip和token
        //获取ip
        String gatewayIpAddress = IpUtil.getIpAddress(requestBody);
        String token = String.valueOf(UUID.randomUUID()).replace("-","");
        UserLoginDTO login = userService.login(userInfoParam, gatewayIpAddress, token);
        return Result.ok(login);
    }

    @GetMapping("/user/logout")
    public Result logout(HttpServletRequest request){
        String token = request.getHeader("token");
        userService.logout(token);
        return Result.ok();
    }
}
